package com.hzqy.web.ac;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hzqy.commons.utils.ConstantUtils;
import com.hzqy.service.catalog.CatalogServiceBean;
import com.hzqy.service.pms.PmsServiceBean;
import com.hzqy.web.vo.CmCatalogVo;
import com.hzqy.web.vo.PmsUserVo;
import com.hzqy.web.vo.PmsZTreeVo;

@Controller
@RequestMapping("/")
public class CatalogAction {
	protected final Logger LOG = LoggerFactory.getLogger(getClass());

	private final String picType = "分类";
	@Resource
	private CatalogServiceBean catalogServiceBean;
	@Resource
	private PmsServiceBean pmsServiceBean;
	
	@RequestMapping("/catalog/catalog_list")
	@ResponseBody
	public Object resourceList(HttpSession session, ModelMap map, CmCatalogVo prv) {
		PmsUserVo userVo = (PmsUserVo) session.getAttribute(ConstantUtils.SESSION_USER);
		//父级组织id为0是根组织,有最高权限 否则必须使用组织本身id覆盖companyid,就看不到别的组织的内容了 
		if(userVo.getParentOrgId() > 0){
			prv.setF_catalog_companyid(userVo.getOrgId());
		}
		List<PmsZTreeVo> ptv = new ArrayList<PmsZTreeVo>();
		ptv = catalogServiceBean.selectCatalogList(prv);
		return ptv;
	}
	
	@RequestMapping("/catalog/delete_catalog")
	@ResponseBody
	public Object deleteCatalog (int f_catalog_id) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		int flag = catalogServiceBean.deleteCatalog(f_catalog_id);
		resultMap.put("resultTitle", "删除结果");
		if(flag>0) {
			resultMap.put("success", true);
			resultMap.put("result","删除成功!");
		} else {
			resultMap.put("success", false);
			resultMap.put("result","删除失败!");
		}
		return resultMap;
	}
	
	@RequestMapping("/catalog/insert_catalog")
	@ResponseBody
	public Object insertCatalog(HttpSession session,ModelMap map,CmCatalogVo cmCatalogVo,String picJson) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		int flag = catalogServiceBean.insertCatalog(cmCatalogVo,picJson);
		if(flag > 0) {
			resultMap.put("success", true);
			resultMap.put("result","新增内容成功!");
		} else {
			resultMap.put("success", true);
			resultMap.put("result","新增内容失败!flag="+flag);
		}
		return resultMap;
	}
	
	@RequestMapping("/catalog/select_catalog")
	@ResponseBody
	public Object selectCatalog(int f_catalog_id) {
		CmCatalogVo cmCatalogVo = catalogServiceBean.selectCatalog(f_catalog_id);
		return cmCatalogVo;
	}
	
	@RequestMapping("/catalog/update_catalog")
	@ResponseBody
	public Object updateCatalog(CmCatalogVo cmCatalogVo, String picJson) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		int flag = catalogServiceBean.updateCatalog(cmCatalogVo,picJson,picType);
		if(flag > 0) {
			resultMap.put("success", true);
			resultMap.put("result","修改内容成功!");
		} else {
			resultMap.put("success", true);
			resultMap.put("result","修改内容失败!flag="+flag);
		}
		return resultMap;
	}
	
	@RequestMapping("/catalog/catalog_industry_list")
	@ResponseBody
	public Object selectCatalogIndustryList(HttpSession session, ModelMap map, CmCatalogVo prv) {
		PmsUserVo userVo = (PmsUserVo) session.getAttribute(ConstantUtils.SESSION_USER);
		//父级组织id为0是根组织,有最高权限 否则必须使用组织本身id覆盖companyid,就看不到别的组织的内容了 
		if(userVo.getParentOrgId() > 0){
			String orgIds = pmsServiceBean.getAuthorityPmsOrgIds(userVo);
			prv.setOrgIds(orgIds);
			prv.setF_catalog_companyid(userVo.getOrgId());
		}
		List<PmsZTreeVo> ptv = new ArrayList<PmsZTreeVo>();
		ptv = catalogServiceBean.selectCatalogIndustryList(prv);
		return ptv;
	}
}
